package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.mapper.mysql.WechatSendRepairUseMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-10-08
 */
@Service
public class WechatSendRepairUseServiceImpl extends ServiceImpl<WechatSendRepairUseMapper, WechatSendRepairUse> implements WechatSendRepairUseService {
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    WechatMaterialDetailService wechatMaterialDetailService;
    @Autowired
    WechatPartsDetailService wechatPartsDetailService;
    @Autowired
    WechatMaterialService wechatMaterialService;
    @Autowired
    WechatPartsService wechatPartsService;
    @Override
    public Boolean insertWechatSendRepairUse(WechatSendRepairUse wechatSendRepairUse, String openid) throws AdscmSystemException {
        wechatSendRepairUse.setCreateTime(new Date());
        //新增对应的出库记录
        if (wechatSendRepairUse.getType()==1){
            //物料
            WechatMaterialDetail wechatMaterialDetail=new WechatMaterialDetail();
            wechatMaterialDetail.setPid(wechatSendRepairUse.getWechatMaterial());
            wechatMaterialDetail.setNums(wechatSendRepairUse.getNums());
            wechatMaterialDetailService.insertWechatMaterialDetail(wechatMaterialDetail,openid);
            wechatSendRepairUse.setWechatMaterialDetail(wechatMaterialDetail.getId());
        }
        if (wechatSendRepairUse.getType()==2){
            //部件
            WechatPartsDetail wechatPartsDetail =new WechatPartsDetail();
            wechatPartsDetail.setPid(wechatSendRepairUse.getWechatParts());
            wechatPartsDetail.setNums(1f);
            wechatPartsDetailService.insertWechatPartsDetail(wechatPartsDetail,openid);
            wechatSendRepairUse.setWechatPartsDetail(wechatPartsDetail.getId());
        }
        boolean insert = super.insert(wechatSendRepairUse);
        return insert;
    }

    @Override
    public List<WechatSendRepairUse> selectByPid(Long pid) {
        Wrapper<WechatSendRepairUse> wechatSendRepairUseWrapper=new EntityWrapper<>();
        wechatSendRepairUseWrapper.eq("pid",pid);
        List<WechatSendRepairUse> wechatSendRepairUses = super.selectList(wechatSendRepairUseWrapper);
        for (WechatSendRepairUse wechatSelfRepairUs : wechatSendRepairUses) {
            if (wechatSelfRepairUs.getType()==1){
                //物料
                if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepairUs.getWechatMaterial(),null)){
                    WechatMaterial wechatMaterial = wechatMaterialService.selectWechatMaterialById(wechatSelfRepairUs.getWechatMaterial());
                    wechatSelfRepairUs.setWechatMaterialName(wechatMaterial);
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepairUs.getWechatMaterialDetail(),null)){
                    WechatMaterialDetail wechatMaterialDetail = wechatMaterialDetailService.selectWechatMaterialDetailById(wechatSelfRepairUs.getWechatMaterialDetail());
                    wechatSelfRepairUs.setWechatMaterialDetailName(wechatMaterialDetail);
                }
            }
            if (wechatSelfRepairUs.getType()==2){
                //部件
                if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepairUs.getWechatParts(),null)){
                    WechatParts wechatParts = wechatPartsService.selectWechatPartsById(wechatSelfRepairUs.getWechatParts());
                    wechatSelfRepairUs.setWechatPartsName(wechatParts);
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepairUs.getWechatPartsDetail(),null)){
                    WechatPartsDetail wechatPartsDetail = wechatPartsDetailService.selectWechatPartsDetailById(wechatSelfRepairUs.getWechatPartsDetail());
                    wechatSelfRepairUs.setWechatPartsDetailName(wechatPartsDetail);
                }
            }
        }
        return wechatSendRepairUses;
    }

    @Override
    public Boolean deleteWechatSendRepairUseById(Long id) throws AdscmSystemException {
        //恢复该对应的部件或者原料的数量
        WechatSendRepairUse wechatSendRepairUse = super.selectById(id);
        if (wechatSendRepairUse.getType()==1){
            //物料数量添加回去
            wechatMaterialService.updateWechatMaterialVaule(wechatSendRepairUse.getWechatMaterial(),-wechatSendRepairUse.getNums());
            //删除对应的出库记录
            wechatMaterialDetailService.deleteById(wechatSendRepairUse.getWechatMaterialDetail());
        }
        if (wechatSendRepairUse.getType()==2){
            //部件
            wechatPartsService.updateWechatPartsVaule(wechatSendRepairUse.getWechatParts(),-1f);
            wechatPartsDetailService.deleteById(wechatSendRepairUse.getWechatPartsDetail());
        }
        boolean b = super.deleteById(id);
        return b ;
    }
}
